<template>
  <div>
    <info-header title="个人信息">
      <div @click="handleEditClick">
        <i class="el-icon-edit"></i>
        编辑
      </div>
    </info-header>
    <div class="content">
      <div class="item">
        <div
          class="head"
          @mouseover="mouseOver"
          @mouseleave="mouseLeave"
        >
          <img
            class="head-img"
            :src="_avatar"
          />
          <div
            class="head-change"
            :class="headChanging"
            @click="handleChangeClick"
          >
            更换头像
          </div>
        </div>
      </div>
      <div class="item">
        <div class="key">账号</div>
        <div class="value">{{_loginName}}</div>
      </div>
      <div class="item">
        <div class="key">昵称</div>
        <div class="value">{{_nickname}}</div>
      </div>
      <div class="item">
        <div class="key">性别</div>
        <div class="value">{{_sex}}</div>
      </div>
      <div class="item">
        <div class="key">个性签名</div>
        <div class="value">{{_motto}}</div>
      </div>
      <div class="item">
        <div class="key">电话</div>
        <div class="value">{{_phone}}</div>
      </div>
      <div class="item">
        <div class="key">邮箱</div>
        <div class="value">{{_email}}</div>
      </div>
    </div>
    <!-- <form-dialog
      title="更换头像"
      :visible.sync="userAvatarDialogVisible"
    >
      <avatar-form
        :userInfo="this.userInfo"
        @cancel="handleAvatarCancel"
        @submit="handleAvatarSubmit"
      />
    </form-dialog> -->
    <el-dialog
      :visible.sync="userAvatarDialogVisible"
      width="500px"
    >
      <dialog-self title="更换头像">
        <div class="dialog-box">
          <avatar-form
            ref="avatarRef"
            :userInfo="this.userInfo"
            @cancel="handleAvatarCancel"
            @submit="handleAvatarSubmit"
          />
        </div>
      </dialog-self>
    </el-dialog>
    <el-dialog
      :visible.sync="userInfoDialogVisible"
      width="500px"
    >
      <dialog-self title="编辑个人信息">
        <div class="dialog-box">
          <info-form
            ref="infoRef"
            :userInfo="this.userInfo"
            @cancel="handleInfoCancel"
            @submit="handleInfoSubmit"
          />
        </div>
      </dialog-self>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from 'vuex'
import InfoForm from './form/InfoForm'
import AvatarForm from './form/AvatarForm'
export default {
  name: 'MyInfo',
  components: {
    InfoForm,
    AvatarForm
  },
  data () {
    return {
      userAvatarDialogVisible: false,
      userInfoDialogVisible: false,
      headChanging: 'head-change-out'
    }
  },
  computed: {
    ...mapState({
      userInfo: (state) => state.user.userInfo
    }),
    _avatar () {
      if (this.userInfo.avatar) {
        return this.userInfo.avatar
      } else {
        return 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
      }
    },
    _loginName () {
      if (this.userInfo.userName) {
        return this.userInfo.loginName
      } else {
        return '暂无账号'
      }
    },
    _nickname () {
      if (this.userInfo.userName) {
        return this.userInfo.userName
      } else {
        return '暂无昵称'
      }
    },
    _sex () {
      if (this.userInfo.sex === '0') {
        return '男'
      } else if (this.userInfo.sex === '1') {
        return '女'
      } else {
        return '保密'
      }
    },
    _motto () {
      if (this.userInfo.motto) {
        return this.userInfo.motto
      } else {
        return '还未设置个性签名'
      }
    },
    _phone () {
      if (this.userInfo.phonenumber) {
        return this.userInfo.phonenumber
      } else {
        return '还未绑定手机号'
      }
    },
    _email () {
      if (this.userInfo.email) {
        return this.userInfo.email
      } else {
        return '还未绑定邮箱'
      }
    }
  },
  methods: {
    ...mapActions({
      getUserInfo: 'user/getUserInfo'
    }),
    ...mapMutations(['updateUserInfo']),
    async handleChangeClick () {
      this.userAvatarDialogVisible = true
      await this.$nextTick()
      this.$refs.avatarRef.reset()
    },
    async handleEditClick () {
      this.userInfoDialogVisible = true
      await this.$nextTick()
      this.$refs.infoRef.reset()
    },
    handleAvatarCancel () {
      this.userAvatarDialogVisible = false
    },
    handleAvatarSubmit () {
      this.userAvatarDialogVisible = false
      this.getUserInfo()
    },
    handleInfoCancel () {
      this.userInfoDialogVisible = false
    },
    handleInfoSubmit () {
      this.userInfoDialogVisible = false
      this.getUserInfo()
    },
    mouseOver () {
      this.headChanging = 'head-change-in'
    },
    mouseLeave () {
      this.headChanging = 'head-change-out'
    }
  }
}
</script>

<style lang="scss" scoped>
.item {
  display: flex;
  padding: 15px 20px;
  .head {
    height: 100px;
    width: 100px;
    border: 4px solid #d0d6d9;
    border-radius: 50%;
    margin: auto;
    position: relative;
    overflow: hidden;
    .head-img {
      height: 100px;
      width: 100px;
    }
    .head-change {
      width: 100%;
      position: absolute;
      padding-bottom: 8px;
      font-size: 12px;
      text-align: center;
      color: #fff;
      background-color: rgba(0, 0, 0, 0.6);
      cursor: pointer;
    }
    .head-change-in {
      bottom: 0px;
      transition: all 0.2s linear;
    }
    .head-change-out {
      bottom: -30px;
      transition: all 0.2s linear;
    }
  }
  .key {
    height: 40px;
    line-height: 40px;
    width: 180px;
    text-align: center;
    background-color: #dadfe6;
    margin-right: 20px;
  }
  .value {
    height: 40px;
    line-height: 40px;
    flex: 1;
    border-bottom: 1px solid #d0d6d9;
    border-right: 1px solid #d0d6d9;
    padding-left: 20px;
    color: #323b49;
  }
}
.dialog-box {
  padding: 20px 20px 30px;
}
</style>
